using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using BoOnce.Components;
using BoOnce.Helpers;
using BoOnce.Core.Components;

/// <TODO>NEED REFRACT !!!</TODO>
namespace BoOnce.GameLogic.Entities
{
    class Dialog : GFX
    {
        public enum DIALOG_TYPE { TEST }

        private /*static*/ Texture2D _MainTexture;
        private /*static*/ Texture2D _Choice1Texture;
        private /*static*/ Texture2D _Choice2Texture;
        private /*static*/ Vector2 PADDING;
        private /*static*/ Vector2 SPACE_BETWEEN_LINE;
        //private /*static*/ Boolean _Init = false;
        private /*static*/ float TEXT_W_MAX;
        private /*static*/ SpriteFont _Font;
        private /*static*/ InputComponent _Input;
        private Vector2 _PositionMain = Vector2.Zero;
        private Vector2 _PositonChoice1 = Vector2.Zero;
        private Vector2 _PositonChoice2 = Vector2.Zero;
        private Boolean _Choice1Selected;
        private Boolean _IsOnQuestionMode;
        private /*static*/ int MAX_NB_LINE = 3;

        private List<DialogEntry> _Lines;
        private Somebody _Entity;
        private int _CurrentLineIndex;
        public Dialog(Somebody entity)
        {
            //if (!_Init)
            //{
                _MainTexture = Game.Content.Load<Texture2D>(Settings.CONTENT_GFX_PATH + "/Dialog/main");
                _Choice1Texture = Game.Content.Load<Texture2D>(Settings.CONTENT_GFX_PATH + "/Dialog/choice1");
                _Choice2Texture = Game.Content.Load<Texture2D>(Settings.CONTENT_GFX_PATH + "/Dialog/choice2");

                _Font = Game.Content.Load<SpriteFont>(Settings.CONTENT_FONT_PATH + "/Yikes!14");
                PADDING = new Vector2(10, 10);
                TEXT_W_MAX = _MainTexture.Width - PADDING.X*2f;
                SPACE_BETWEEN_LINE = new Vector2(0, 20f);
                _Input = (InputComponent)Game.Services.GetService(typeof(InputComponent));
            //    _Init = true;
            //}
            _IsOnQuestionMode = false;
            _Choice1Selected = true;
            _CurrentLineIndex = 0;
            _Lines = new List<DialogEntry>();

            _Entity = entity;

            _Lines.Add(new DialogSimpleText("premiere line"));
            _Lines.Add(new DialogChoice("Tu veux me donner a bouffer?", "va te faire", "non", OnChoice1, OnChoice2));

        }

        private void OnChoice1(object sender, EventArgs e)
        {
            Logger.WriteInfo("choice1");
        }

        private void OnChoice2(object sender, EventArgs e)
        {
            Logger.WriteInfo("choice2");
        }

        public void AddLine(String line)
        {
            _Lines.Add(new DialogSimpleText(line));
        }

        public void AddChoice(String question, String choice1, String choice2, EventHandler<EventArgs> handleChoice1, EventHandler<EventArgs> handleChoice2)
        {
            _Lines.Add(new DialogChoice(question, choice1, choice2, handleChoice1, handleChoice2));
        }

        #region DrawableEntity
        public override void QuickDraw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(_MainTexture, Camera.ToScreenPosition(_PositionMain), Color.White);
            int iMax = _Lines.Count;
            for(int i=_CurrentLineIndex; i < _CurrentLineIndex + MAX_NB_LINE;  i++){
                if (i < iMax)
                {
                    spriteBatch.DrawString(_Font, _Lines[i].Text, Camera.ToScreenPosition(_PositionMain + PADDING + SPACE_BETWEEN_LINE * (i - _CurrentLineIndex)), Color.Black);
                    _IsOnQuestionMode = _Lines[i].IsQuestion; 
                    if (_IsOnQuestionMode)
                    {
                        DialogChoice choice = (DialogChoice)_Lines[i];
                        if (_Choice1Selected)
                        {
                            spriteBatch.Draw(_Choice1Texture, Camera.ToScreenPosition(_PositonChoice1), Color.Red);
                            spriteBatch.DrawString(_Font, choice.Choice1, Camera.ToScreenPosition(_PositonChoice1 + PADDING), Color.Black);

                            spriteBatch.Draw(_Choice2Texture, Camera.ToScreenPosition(_PositonChoice2), Color.White);
                            spriteBatch.DrawString(_Font, choice.Choice2, Camera.ToScreenPosition(_PositonChoice2 + PADDING), Color.Black);
                        }
                        else
                        {
                            spriteBatch.Draw(_Choice1Texture, Camera.ToScreenPosition(_PositonChoice1), Color.White);
                            spriteBatch.DrawString(_Font, choice.Choice1, Camera.ToScreenPosition(_PositonChoice1 + PADDING), Color.Black);

                            spriteBatch.Draw(_Choice2Texture, Camera.ToScreenPosition(_PositonChoice2), Color.Red);
                            spriteBatch.DrawString(_Font, choice.Choice2, Camera.ToScreenPosition(_PositonChoice2 + PADDING), Color.Black);
                        }
                        _CurrentLineIndex = i;
                        break;
                    }
                }
            }
            if (_CurrentLineIndex + MAX_NB_LINE < iMax)
            {
                spriteBatch.DrawString(_Font, "Next ...", Camera.ToScreenPosition(_PositionMain + PADDING + SPACE_BETWEEN_LINE * (MAX_NB_LINE + 1)), Color.Black); 
            }
           
        }

        public override void Update(GameTime gameTime)
        {
            // update position
            _PositionMain.X = _Entity.Position.X + (_Entity.Width * 0.10f);
            _PositionMain.Y = _Entity.Position.Y - (_Entity.Height * 0.10f) - _MainTexture.Height;

            _PositonChoice1.X = _Entity.Position.X - (_Entity.Width * 0.30f) - _Choice1Texture.Width;
            _PositonChoice1.Y = _Entity.Position.Y - (_Entity.Height * 0.10f) - _Choice1Texture.Height - 10;

            _PositonChoice2.X = _Entity.Position.X - (_Entity.Width * 0.30f) - _Choice2Texture.Width;
            _PositonChoice2.Y = _Entity.Position.Y - (_Entity.Height * 0.10f) - _Choice2Texture.Height + 40;

            if (_IsOnQuestionMode)
            {
                if (_Input.IsNewPress(PlayerIndex.One, InputComponent.Buttons.Down))
                {
                    _Choice1Selected = false;
                }

                if (_Input.IsNewPress(PlayerIndex.One, InputComponent.Buttons.Up))
                {
                    _Choice1Selected = true;
                }

                if (_Input.IsNewPress(PlayerIndex.One, InputComponent.Buttons.Action1))
                {
                    DialogChoice choice = (DialogChoice)_Lines[_CurrentLineIndex];
                    if (_Choice1Selected)
                        choice.OnSelectChoice1();
                    else
                        choice.OnSelectChoice2();
                }
            }

            if (!_IsOnQuestionMode && _Input.IsNewPress(PlayerIndex.One,InputComponent.Buttons.Action1))
            {
                _CurrentLineIndex += MAX_NB_LINE;
            }
        }
        #endregion
    }

    class DialogEntry
    {
        public String Text;
        public Boolean IsQuestion;
    }

    class DialogChoice : DialogEntry
    {
        public String Choice1;
        public String Choice2;
        private event EventHandler<EventArgs> Choice1Selected;
        private event EventHandler<EventArgs> Choice2Selected;

        public void OnSelectChoice1()
        {
            if (Choice1Selected != null)
                Choice1Selected(this, EventArgs.Empty);
        }

        public void OnSelectChoice2()
        {
            if (Choice2Selected != null)
                Choice2Selected(this, EventArgs.Empty);
        }

        public DialogChoice(String question, String one, String two, EventHandler<EventArgs> choice1Selected, EventHandler<EventArgs> choice2Selected)
        {
            Choice1Selected += choice1Selected;
            Choice2Selected += choice2Selected;
            IsQuestion = true;
            Text = question;
            Choice1 = one;
            Choice2 = two;
        }
    }

    class DialogSimpleText : DialogEntry
    {
        public DialogSimpleText(String txt)
        {
            IsQuestion = false;
            Text = txt;
        }
    }
}
